package com.cindata

class FileUtil {
    static String GetFileCharset(InputStream inputStream) {
        String charset = "GBK"
        byte[] first3Bytes = new byte[3]
        try {
            BufferedInputStream bis = new BufferedInputStream(inputStream)
            bis.mark(0)
            int read = bis.read(first3Bytes, 0, 3)
            System.out.println("字节大小："+read)
            if (read == -1) {
                return charset //文件编码为 ANSI
            } else if (first3Bytes[0] == (byte) 0xFF
                    && first3Bytes[1] == (byte) 0xFE) {
                charset = "UTF-16LE" //文件编码为 Unicode
            } else if (first3Bytes[0] == (byte) 0xFE
                    && first3Bytes[1] == (byte) 0xFF) {
                charset = "UTF-16BE" //文件编码为 Unicode big endian
            } else if (first3Bytes[0] == (byte) 0xEF
                    && first3Bytes[1] == (byte) 0xBB
                    && first3Bytes[2] == (byte) 0xBF) {
                charset = "UTF-8" //文件编码为 UTF-8
            }
            bis.reset()
            bis.close()
        } catch (Exception e) {
            e.printStackTrace()
        }
        return charset
    }
}
